package modelo;

import java.sql.ResultSet;
import java.sql.SQLException;

public class FrasesDao {

	public FrasesDao() {
		super();
	}
	
	public void insertarFrase (Frases frase)
	{
		
		String tiraSQL = "INSERT INTO frase " +
					     "(nombre,"+"nivel)"
		+ "VALUES" + "('"+frase.getNombre()+"'," +"'"+frase.getNivel()+"')";
		Conexion.ejecutar(tiraSQL);
	}
	
	public void insertarFraseIntermedia (Frases frase)
	{
		
		String tiraSQL = "INSERT INTO frase " +
					     "(nombre,"+"nivel)"
		+ "VALUES" + "('"+frase.getNombre()+"'," +"'"+frase.getNivel()+"')";
		Conexion.ejecutar(tiraSQL);
	}
	
	public void insertarFraseAvanzada (Frases frase)
	{
		
		String tiraSQL = "INSERT INTO frase " +
					     "(nombre,"+"nivel)"
		+ "VALUES" + "('"+frase.getNombre()+"'," +"'"+frase.getNivel()+"')";
		Conexion.ejecutar(tiraSQL);
	}

	public int totalFrases (int nivel)
	{
		int cont=0;
		String tiraSQL = "SELECT * FROM frase where nivel = '"+nivel+"' ";
		ResultSet resulSet = Conexion.consultar(tiraSQL);
		try{
			while (resulSet.next())
			 cont++;
		}catch (SQLException e){
			e.printStackTrace();
		}
		return cont;
	}
	
	
	public String fraseAleatorea ( int nivel)
	{
		String tiraSQL = new String();
		int cont=0;
		String frase= null;
		tiraSQL = "SELECT * FROM frase where nivel = '"+nivel+"' ";
		ResultSet resulSet = Conexion.consultar(tiraSQL);
		try{
			while(resulSet.next())
			{
			 cont++;
			 	if (cont==numeroAleatoria(nivel))
			 	 frase = resulSet.getString("nombre");	
			}
		}catch (SQLException e){
			e.printStackTrace();
		}
		return frase;
		
	}
	
	
	public int numeroAleatoria (int nivel)
	{
		int num=0;
		if (nivel==1)
		{
			 num =(int)Math.floor(Math.random()*(totalFrases(nivel)+1));
	
		}		
		else if  (nivel==2)
		{
			 num =(int)Math.floor(Math.random()*(totalFrases(nivel)+1));
		}
		else if  (nivel==3)
		{
			 num =(int)Math.floor(Math.random()*(totalFrases(nivel)+1));
		}
		return num;
	}
	

}
